
"use strict";

angular.module('app.user').controller('PerUserCtrl', function ($scope,$http,$cookieStore) {
    $scope.fid = $cookieStore.get('userinfo').fid;
    $scope.purview = false;
    if($scope.fid !== "0" && $scope.fid !== "1"){
        $scope.purview = true;
    }
      //配置分页基本参数
    $scope.paginationConf = {
        currentPage: 1,
        itemsPerPage: 10,
        pagesLength: 5,
        totalItems:20,
        perPageOptions: [10, 20, 30, 40, 50],
        onChange: function(){
        }
    };
    //获得用户个人信息
    $http.get(baseurl+'/session_user').then(function(res){
         // console.log(res.data);
         if (res.data.ret == 1) {
            if (res.data.info.pic == null || res.data.info.pic == '') {
              res.data.info.pic = "styles/img/avatars/avatar.jpg";
            }
            if (res.data.info.warn_grade == -1) {
              res.data.info.warn_grade_name = "无";
            }else{
              res.data.info.warn_grade_name = res.data.info.warn_grade;
            }
            $scope.user = res.data.info;
         }
    });
    // 手机号验证
    function checkMobile(sMobile){ 
        if(!(/^1[3|4|5|8][0-9]\d{4,8}$/.test(sMobile))){ 
            return false; 
        }else{
            return true;
        }
    } 
    // 修改个人资料
    $scope.edit_curuser = function(){
         // console.log($scope.user);
         $("#editAlert").modal("show");
         $scope.editobj = {
             id:$scope.user.id,
             clientname:$scope.user.clientname,
             name:$scope.user.name,
             warn_grade:$scope.user.warn_grade,
             tele:$scope.user.tele
         };
    };
    $scope.sure_edit = function(){
        if($scope.editobj.tele !== null && $scope.editobj.tele !== ''){
            if(checkMobile($scope.editobj.tele) === false){
               new Notify('info','手机号码输入错误'); 
               return;
            }           
        }
        $http.put(baseurl+'/data/user?id=='+$scope.editobj.id,$scope.editobj).then(function(res){
              // console.log(res.data);
              if (res.data.ret === 1) {
                 $("#editAlert").modal("hide");
                 new Notify('success','修改成功');
                 $http.get(baseurl+'/session_user').then(function(res){
                     // console.log(res.data);
                     if (res.data.ret === 1) {
                        if (res.data.info.pic === null || res.data.info.pic === '') {
                          res.data.info.pic = "styles/img/avatars/avatar.jpg";
                        }
                        if (res.data.info.warn_grade == -1) {
                          res.data.info.warn_grade_name = "无";
                        }else{
                          res.data.info.warn_grade_name = res.data.info.warn_grade;
                        }
                        $scope.user = res.data.info;
                     }
                })
              }
        });
    };

});